<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class GameLogRepository {
    /**
     * 获取新钓鱼礼物详情列表
     * @param $where
     * @return bool
     */
    public function list($where = []) {
        $list = app('model_game_log')->where($where)->select('gift_id', DB::raw('sum(total) as total_gift'), DB::raw('sum(number) as total_count'))->groupBy('gift_id')->get()->toArray();
        return $list;
    }

    /*
     * 获取列表
     * @param $param
     * @return bool
     * @throws BasicException
     * */

    public function getGameLogList($where_game = [], $keyword = '', $page = 1, $perPage = 20) {
        $getGameLogList = app('model_game_log')
            ->with(['gift:id,name,price', 'user_data:id,user_id,user_code,nickname'])
            ->orderBy('id', 'desc')
            ->where($where_game)
            ->where(function ($query) use ($keyword) {
                $query->whereHas('user_data', function ($query) use ($keyword) {
                    $query->where('user_code', 'like', "%{$keyword}%")->orWhere('nickname', 'like', "%{$keyword}%");
                });
            })
            ->paginate($perPage, ['*'], '', $page);
        return $getGameLogList;
    }


    /*
    * 日汇总查询
    * @return bool
    * */
    public function userDailySummary() {
        $userDailySummary = app('model_game_log')
            ->select('user_id', 'pool_type', DB::raw('sum(number) as number'), DB::raw('sum(total) as total'), DB::raw('sum(cost) as cost'), DB::raw('FROM_UNIXTIME(`add_time`,\'%Y-%m-%d\') as summary_date'))
            ->groupBy('summary_date')
            ->groupBy('user_id')
            ->groupBy('pool_type')
            ->get()
            ->toArray();
        return $userDailySummary;
    }
}


?>